package com.zx.cloud.job;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.zx.cloud.SpringUtils;
import com.zx.cloud.domain.ScheduleJob;
import com.zx.cloud.domain.ScheduleJobHistory;
import com.zx.cloud.domain.enums.TaskStatusEnum;
import com.zx.cloud.persistence.ScheduleJobHistoryMapper;

public class TaskUtils {

	 private static final Logger LOGGER = LoggerFactory.getLogger(TaskUtils.class);

	/**
	 * 通过反射调用scheduleJob中定义的方法
	 */
	public static void invokMethod(ScheduleJob scheduleJob) {
		LOGGER.debug("执行队列[{}]中任务[{}].",scheduleJob.getJobGroup(),scheduleJob.getJobName());
		String msString="执行失败,反射执行错误";
		boolean hasError=false;
		Object object = null;
		Class<?> clazz = null;
		if (!StringUtils.isEmpty(scheduleJob.getClassName())) {
			try {
				clazz = Class.forName(scheduleJob.getClassName());
				object = clazz.newInstance();
			} catch (Exception e) {
				hasError=true;
				e.printStackTrace();
			}
		}
		if (object == null) {
			msString="请检查是否配置正确";
			LOGGER.error("任务名称 = [" + scheduleJob.getJobName() + "]---------------未启动成功，请检查是否配置正确!");
			return;
		}
		clazz = object.getClass();
		Method method = null;
		try {
			method = clazz.getDeclaredMethod(scheduleJob.getMethodName(),ScheduleJob.class);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			hasError=true;
			LOGGER.error("任务名称 = [" + scheduleJob.getJobName() + "]------MethodName["+scheduleJob.getMethodName()+"]---clazz["+clazz+"]------未启动成功，方法名设置错误!");
		} catch (SecurityException e) {
			hasError=true;
			e.printStackTrace();
		}
		if (!hasError&&method != null) {
			try {
				long currentTime=System.currentTimeMillis();
				method.invoke(object,scheduleJob);
				msString="执行成功";
				LOGGER.debug("任务名称 = [" + scheduleJob.getJobName() + "]----------启动成功!");
				ScheduleJobHistoryMapper scheduleJobHistoryMapper=SpringUtils.getBean(ScheduleJobHistoryMapper.class);
				ScheduleJobHistory scheduleJobHistory=new ScheduleJobHistory();
				scheduleJobHistory.setDesc(msString);
				scheduleJobHistory.setUseTime(System.currentTimeMillis()-currentTime);
				scheduleJobHistory.setStatus(TaskStatusEnum.NORMAL);
				scheduleJobHistory.setGroubName(scheduleJob.getJobGroup());
				scheduleJobHistory.setJobName(scheduleJob.getJobName());
				scheduleJobHistoryMapper.insert(scheduleJobHistory);
			} catch (IllegalAccessException e) {
				hasError=true;
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				hasError=true;
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				hasError=true;
				e.printStackTrace();
			} catch (Exception e) {
				hasError=true;
				e.printStackTrace();
			}
		}
		if(hasError){
			ScheduleJobHistoryMapper scheduleJobHistoryMapper=SpringUtils.getBean(ScheduleJobHistoryMapper.class);
			ScheduleJobHistory scheduleJobHistory=new ScheduleJobHistory();
			scheduleJobHistory.setDesc(msString);
			scheduleJobHistory.setStatus(TaskStatusEnum.ERROR);
			scheduleJobHistory.setGroubName(scheduleJob.getJobGroup());
			scheduleJobHistory.setJobName(scheduleJob.getJobName());
			scheduleJobHistoryMapper.insert(scheduleJobHistory);
		}
	}
}
